#include "server.h"

pthread_mutex_t lock;
void UDP_Process(void)
{
    
    pthread_t Pid[128];
    int UDP_fd = UDP_init(atoi("9999"), "192.168.10.234");
    struct sockaddr_in * pclientaddr =(struct sockaddr_in * )malloc(sizeof(struct sockaddr_in));
    struct sockaddr_in clientaddr; // 记录客户端信息  用于回发
    memset(&clientaddr, 0, sizeof(clientaddr));
    socklen_t addrlen;
    UDP_data datapack;
    Thread_Send clientdata;
    memset(&clientdata, 0, sizeof(clientdata));
    int index = 0;
    
    pthread_mutex_init(&lock , NULL); 
    int  client_count=0;
    while (1)
    {

        memset(&datapack, 0, sizeof(datapack));
        // memset(&clientdata, 0, sizeof(clientdata));
        if (-1 == recvfrom(UDP_fd, &datapack, sizeof(datapack), 0 , (struct sockaddr *)pclientaddr , &addrlen)) // 阻塞等待信息
        {
            ERRLOG("接收失败");
        }
        clientaddr = (*pclientaddr);
        // 接收成功
        printf("[%s - %d]\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
        clientdata.UDP_fd = UDP_fd;
        clientdata.clientaddr = clientaddr;
        clientdata.addrlen = addrlen;
        clientdata.data = datapack;
        // 打印信息源的发送内容和IP地址以及端口号
        
        printf("<<<<<<SYN=|%d|\tname=|%s|\ndata=|%s|\nsize_num=|%d|\ttime=|%s|\n\n", datapack.SYN, datapack.name, datapack.data, datapack.size_num, datapack.time); // 打印数据包内容
        printf("clientdata.data:<<<<<<SYN=|%d|\tname=|%s|\ndata=|%s|\nsize_num=|%d|\ttime=|%s|\n\n", clientdata.data.SYN, clientdata.data.name, clientdata.data.data, clientdata.data.size_num, clientdata.data.time);
        // 请求服务器  发送 群文件目录
        switch (datapack.SYN)
        {
        case 1: // 响应请求1  向客户端发送所在群的所有文件名称

            pthread_create(&Pid[index], NULL, UDP_reply_1, (void *)&clientdata  );
            pthread_detach(Pid[index]);


            break;
        case 2: // 响应请求2 接收客户端所给文件名 向客户端发送文件内容

            pthread_create(&Pid[index], NULL, UDP_reply_2, (void *)&clientdata);
            pthread_detach(Pid[index]);
            break;
        case 3://响应群聊  
     
            pthread_create(&Pid[index], NULL, UDP_reply_3, (void *)&clientdata);
            pthread_detach(Pid[index]);

            break;

        case 4: // 响应登录
          
            pthread_create(&Pid[index], NULL, UDP_reply_4, (void *)&clientdata);
            pthread_detach(Pid[index]);
            break;
        case 5: // 响应注册
      
            pthread_create(&Pid[index], NULL, UDP_reply_5, (void *)&clientdata);
            pthread_detach(Pid[index]);
            break;
        case 6: // 响应找回密码
     
            pthread_create(&Pid[index], NULL, UDP_reply_6, (void *)&clientdata);
            pthread_detach(Pid[index]);
            break;

        default:
            break;
        }

        if (index == 128)
        {
            index = 0;
        }
        index++;
    }
    pthread_mutex_destroy(&lock);
    UDP_close(UDP_fd);
}

/*  UDP初始化
1、创建用户数据报 套接字
2、填充服务器网络信息结构体
3、将套接字与服务器的网络信息结构体绑定
*/
int UDP_init(int port_num, char *IP_addr)
{

    struct sockaddr_in serveraddr;
    int soc = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == soc)
    {
        ERRLOG("UDP socket error");
        return -1;
    }

    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(port_num);           // 端口号
    serveraddr.sin_addr.s_addr = inet_addr(IP_addr); // ip地址
    socklen_t addrlen = sizeof(serveraddr);

    // 绑定
    if (-1 == bind(soc, (struct sockaddr *)&serveraddr, addrlen))
    {
        ERRLOG("bind error");
        return -1;
    }

    return soc;
}

// 将文件目录打包到发送包
UDP_data send_file_dir(char *file_dir_name, UDP_data file_dir_pack)
{

    DIR *dir = opendir(file_dir_name);
    char file_path[128];
    // f判断目录能否正常打开
    if (dir == NULL)
    {
        perror("无法打开目录");
        return file_dir_pack;
    }
    // 操作文件
    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL)
    {
        // 如果这个文件是目录 且不是  ”.“   或者”..“  则进入递归
        if (entry->d_type == DT_DIR && strcmp(entry->d_name, "..") && strcmp(entry->d_name, "."))
        {
            // 将文件路径拼接完整
            snprintf(file_path, sizeof(file_path), "%s/%s", file_dir_name, entry->d_name);
            // printf("进入下一级目录file_path=%s\n",file_path);
            file_dir_pack = send_file_dir(file_path, file_dir_pack);
        }
        if (strcmp(entry->d_name, "..") && strcmp(entry->d_name, "."))
        {

            strcat(file_dir_pack.data, entry->d_name);

            file_dir_pack.data[strlen(file_dir_pack.data)] = '-';
            file_dir_pack.data[strlen(file_dir_pack.data) + 1] = '\0';
            file_dir_pack.size_num++;
        }
    }
    return file_dir_pack;
}

/*              响应请求1  向客户端发送所在群的所有文件名称                             */
void *UDP_reply_1(void *val)
{
    pthread_mutex_lock(&lock);

    Thread_Send CliAddr = *(Thread_Send *)val;
    // printf("[%s - %d]\n", inet_ntoa(CliAddr.clientaddr.sin_addr), ntohs(CliAddr.clientaddr.sin_port));
    UDP_data datapack;
    memset(&datapack, 0, sizeof(datapack));
    datapack.SYN = 1;
    strcpy(datapack.name, "服务器");
    time_t current_time;
    time(&current_time);
    struct tm *local_time = localtime(&current_time);
    strftime(datapack.time, sizeof(datapack.time), "%m/%d %H:%M:%S", local_time);
    datapack = send_file_dir(FILE_DIR, datapack); // 打包数据包
    if (-1 == sendto(CliAddr.UDP_fd, &datapack, sizeof(datapack), 0, (struct sockaddr *)&CliAddr.clientaddr, CliAddr.addrlen))
    {
        ERRLOG("发送失败  \n");
    }
   pthread_mutex_unlock(&lock); 
    return NULL;
}

/*              响应请求2 接收客户端所给文件名 向客户端发送文件内容       */
void *UDP_reply_2(void *val)
{
     pthread_mutex_lock(&lock);
    char file_name[256] = "my_file/聊天室项目讨论群/";
    Thread_Send CliAddr = *(Thread_Send *)val;
    strcat(file_name, CliAddr.data.data);
    UDP_data datapack;
    memset(&datapack, 0, sizeof(datapack));
    datapack.SYN = 3;
    strcpy(datapack.name, "服务器");
 
    time_t current_time;
    time(&current_time);
    struct tm *local_time = localtime(&current_time);
    strftime(datapack.time, sizeof(datapack.time), "%m/%d %H:%M:%S", local_time);
    FILE *fp = fopen(file_name, "r");
    fseek(fp, CliAddr.data.size_num, SEEK_SET);
    if (0 >= fread(datapack.data, 1, sizeof(datapack.data) - 1, fp))
    {
        // 发送完成
        strcpy(datapack.data, "文件发送完成");

        printf(">>>>>|%d|%s|%s|%d|%s|\n", datapack.SYN, datapack.name, datapack.data, datapack.size_num, datapack.time);
        if (-1 == sendto(CliAddr.UDP_fd, &datapack, sizeof(datapack), 0, (struct sockaddr *)&CliAddr.clientaddr, CliAddr.addrlen))
        {
            ERRLOG("发送失败  \n");
        }
        fclose(fp);
          pthread_mutex_unlock(&lock); 
        return NULL;
    }

    datapack.size_num = CliAddr.data.size_num + strlen(datapack.data);
    printf(">>>>>|%d|%s|%s|%d|%s|\n", datapack.SYN, datapack.name, datapack.data, datapack.size_num, datapack.time);
    if (-1 == sendto(CliAddr.UDP_fd, &datapack, sizeof(datapack), 0, (struct sockaddr *)&CliAddr.clientaddr, CliAddr.addrlen))
    {
        ERRLOG("发送失败  \n");
    }
    fclose(fp);
  pthread_mutex_unlock(&lock); 
    return NULL;
}
struct sockaddr_in  client_addrs[MAX_CLIENTS];
int client_count ; 
// 移除退出的客户端
void remove_client(struct sockaddr_in *client_addr)
{
    for (int i = 0; i < client_count; i++)
    {
        if (client_addrs[i].sin_addr.s_addr == client_addr->sin_addr.s_addr &&
            client_addrs[i].sin_port == client_addr->sin_port)
        {
            for (int j = i; j < client_count - 1; j++)
            {
                client_addrs[j] = client_addrs[j + 1];
            }
            client_count--;
            break;
        }
    }
}




/*              响应请求3 群聊       */
void *UDP_reply_3(void *val)
{

    Thread_Send group_user = *((Thread_Send *)val);


    // 检查客户端是否已存在
    int known_client = 0;
    for (int i = 0; i < client_count; i++)
    {
        if (client_addrs[i].sin_addr.s_addr == group_user.clientaddr.sin_addr.s_addr &&
            client_addrs[i].sin_port == group_user.clientaddr.sin_port)
        {
            known_client = 1;
            break;
        }
    }

    // 如果是新客户端，发送欢迎消息
    if (!known_client && client_count < MAX_CLIENTS)
    {
        client_addrs[client_count++] = group_user.clientaddr;

        // 发送欢迎语并告知所有其他客户端
        send_welcome_message(group_user.UDP_fd, &group_user.data);

    }

    // 检查是否是退出消息
    if (strcmp(group_user.data.data, "quit") == 0)
    {
        printf("%s 已退出群聊\n", group_user.data.name);
        // 群发退出消息
        send_exit_message(group_user.UDP_fd, &group_user.data);
        remove_client(&group_user.clientaddr);
    }

    // 群发消息给所有客户端
    printf("转发%s的消息: %s\n", group_user.data.name, group_user.data.data);
    for (int i = 0; i < client_count; i++)
    {
        if(-1 ==  sendto(group_user.UDP_fd, &group_user.data, sizeof(group_user.data), 0, (struct sockaddr *)&client_addrs[i], sizeof(client_addrs[i])))
        {
            printf("发送失败");
        }
    }

    printf("收到来自%s的消息: %s\n", group_user.data.name, group_user.data.data);

    return NULL;
}
// 发送欢迎信息给所有客户端
void send_welcome_message(int server_fd, UDP_data *client_data)
{
    UDP_data welcome_data;
    memset(&welcome_data, 0, sizeof(welcome_data));
    sprintf(welcome_data.data, "%s欢迎 %s 加入群聊！%s",YELLOW, client_data->name,RESET);
    welcome_data.SYN = 3;

    // 群发欢迎消息
    for (int i = 0; i < client_count; i++)
    {
        sendto(server_fd, &welcome_data, sizeof(welcome_data), 0, (struct sockaddr *)&client_addrs[i], sizeof(client_addrs[i]));
    }
}

// 发送退出消息给所有客户端
void send_exit_message(int server_fd, UDP_data *client_data)
{
    UDP_data exit_data;
    memset(&exit_data, 0, sizeof(exit_data));
    sprintf(exit_data.data, "%s%s 已退出群聊！%s", RED,client_data->name,RESET);
    exit_data.SYN = 3;

    // 群发退出消息
    for (int i = 0; i < client_count; i++)
    {
        sendto(server_fd, &exit_data, sizeof(exit_data), 0, (struct sockaddr *)&client_addrs[i], sizeof(client_addrs[i]));
    }
}


/*              响应请求4 接收客户端所给账号密码 验证账号密码的正确性       */
void *UDP_reply_4(void *val)
{

    pthread_mutex_lock(&lock);
    Thread_Send CliAddr = *(Thread_Send *)val;
   
    user_t userinfo;
    UDP_data datapack;
    memset(&datapack, 0, sizeof(datapack));
    int count=0;
    size_t j;
    for (size_t i = 0; i < strlen(CliAddr.data.data); i++)
    {
        if (CliAddr.data.data[i] == ':')
        {
            count++;
           
            switch (count)
            {
            case 1:

                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.name[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.name[j]='\0';
                break;
            case 2:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.account[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.account[j]='\0';
                break;
            case 3:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.passwd[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.passwd[j]='\0';
                break;

            case 4:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.phone[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.phone[j]='\0';
                break;
            case 5:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.sex[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.sex[j]='\0';
                break;
            case 6:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.status = CliAddr.data.data[i + j + 1] ;
                }
                break;
            }
        }
        
    }


    Print_user_data(userinfo);
    printf("[%s - %d]\n", inet_ntoa(CliAddr.clientaddr.sin_addr), ntohs(CliAddr.clientaddr.sin_port));
    userinfo=find_userinfo(userinfo);//没找到
    printf("*******%d\n", userinfo.status);
    if(userinfo.status == -1 ) 
    {
        datapack.SYN = -1;
        memset(&datapack.data,0 ,sizeof(datapack.data));
    }
    else
    {
       datapack.SYN = 4; 
       printf("正确\n");
       sprintf(datapack.data,"姓名:%s 账号:%s 密码:%s 手机号:%s 性别:%s 等级:%d ", userinfo.name , userinfo.account, userinfo.passwd ,userinfo.phone ,userinfo.sex ,userinfo.status);
    }
   
    strcpy(datapack.name, "服务器");
    time_t current_time;
    time(&current_time);
    struct tm *local_time = localtime(&current_time);
    strftime(datapack.time, sizeof(datapack.time), "%m/%d %H:%M:%S", local_time);
    datapack.size_num=strlen(datapack.data);
    printf(">>>>>|%d|%s|%s|%d|%s|\n", datapack.SYN, datapack.name, datapack.data, datapack.size_num, datapack.time);
   
    if (-1 == sendto(CliAddr.UDP_fd, &datapack, sizeof(datapack), 0, (struct sockaddr *)&CliAddr.clientaddr, CliAddr.addrlen))
    {
        ERRLOG("发送失败  \n");
    }
pthread_mutex_unlock(&lock);
    return NULL;
}

/*              响应请求5 注册账号       */
void *UDP_reply_5(void *val)
{
    pthread_mutex_lock(&lock);
    Thread_Send CliAddr = *(Thread_Send *)val;
   
    user_t userinfo;
    UDP_data datapack;
    memset(&datapack, 0, sizeof(datapack));
    int count=0;
    size_t j;
    for (size_t i = 0; i < strlen(CliAddr.data.data); i++)
    {
        if (CliAddr.data.data[i] == ':')
        {
            count++;
           
            switch (count)
            {
            case 1:

                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.name[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.name[j]='\0';
                break;
            case 2:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.account[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.account[j]='\0';
                break;
            case 3:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.passwd[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.passwd[j]='\0';
                break;

            case 4:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.phone[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.phone[j]='\0';
                break;
            case 5:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.sex[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.sex[j]='\0';
                break;
            case 6:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.status = CliAddr.data.data[i + j + 1] ;
                }
                break;
            }
        }
        
    }


    Print_user_data(userinfo);
    printf("[%s - %d]\n", inet_ntoa(CliAddr.clientaddr.sin_addr), ntohs(CliAddr.clientaddr.sin_port));
    if ( -1 == pos_insert_LinkList(userinfo , 1))  //插入到链表中
    {
        datapack.SYN = -1;
        ERRLOG("注册失败，该账号已经注册过");
        printf(">>>>>|%d|%s|%s|%d|%s|\n", datapack.SYN, datapack.name, datapack.data, datapack.size_num, datapack.time);
        if (-1 == sendto(CliAddr.UDP_fd, &datapack, sizeof(datapack), 0, (struct sockaddr *)&CliAddr.clientaddr, CliAddr.addrlen))
        {
            ERRLOG("发送失败  \n");
        }

        pthread_mutex_unlock(&lock);
        return NULL;
    }

    f_Sign_Print(userinfo);// 追加写入文件
     printf("\n-写入用户文件成功--------\n");
    FriendList user_data;
    strcpy( user_data.user_name , userinfo.name);
    strcpy( user_data.firend , userinfo.name);
     printf("\n-开始写入好友文件-------\n");
    friend_f_add_Print(  user_data ) ;
     printf("\n-开始写入好友文件成功-------\n");
    printf("追加写入文件成功%d\n", userinfo.status);
    datapack.SYN = 5; 
    ERRLOG("注册成功\n");
    strcpy(datapack.data, "注册成功");
    strcpy(datapack.name, "服务器");
    time_t current_time;
    time(&current_time);
    struct tm *local_time = localtime(&current_time);
    strftime(datapack.time, sizeof(datapack.time), "%m/%d %H:%M:%S", local_time);
    datapack.size_num=strlen(datapack.data);
    printf("注册成功回发数据包>>>>>|%d|%s|%s|%d|%s|\n", datapack.SYN, datapack.name, datapack.data, datapack.size_num, datapack.time);
    if (-1 == sendto(CliAddr.UDP_fd, &datapack, sizeof(datapack), 0, (struct sockaddr *)&CliAddr.clientaddr, CliAddr.addrlen))
    {
        ERRLOG("发送失败  \n");
    }

    pthread_mutex_unlock(&lock);
    return NULL;
}
/*              响应请求6 找回密码       */
void *UDP_reply_6(void *val)
{
    pthread_mutex_lock(&lock);
    Thread_Send CliAddr = *(Thread_Send *)val;
   
    user_t userinfo;
    UDP_data datapack;
    memset(&datapack, 0, sizeof(datapack));
    int count=0;
    size_t j;
    for (size_t i = 0; i < strlen(CliAddr.data.data); i++)
    {
        if (CliAddr.data.data[i] == ':')
        {
            count++;
           
            switch (count)
            {
            case 1:

                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.name[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.name[j]='\0';
                break;
            case 2:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.account[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.account[j]='\0';
                break;
            case 3:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.passwd[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.passwd[j]='\0';
                break;

            case 4:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.phone[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.phone[j]='\0';
                break;
            case 5:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.sex[j] = CliAddr.data.data[i + j + 1];
                }
                userinfo.sex[j]='\0';
                break;
            case 6:
                for ( j = 0; CliAddr.data.data[i + j + 1] != ' '; j++)
                {
                    userinfo.status = CliAddr.data.data[i + j + 1] ;
                }
                break;
            }
        }
        
    }


    Print_user_data(userinfo);
    printf("[%s - %d]\n", inet_ntoa(CliAddr.clientaddr.sin_addr), ntohs(CliAddr.clientaddr.sin_port));
    userinfo=find_userinfo_passwd(userinfo);//返回这个账号对应的账号信息
    printf("*******%d\n", userinfo.status);
    if(userinfo.status == -1 ) 
    {
        datapack.SYN = -1;
        memset(&datapack.data,0 ,sizeof(datapack.data));
    }
    else
    {
       datapack.SYN = 6; 
       printf("正确\n");
       sprintf(datapack.data,"姓名:%s 账号:%s 密码:%s 手机号:%s 性别:%s 等级:%d ", userinfo.name , userinfo.account, userinfo.passwd ,userinfo.phone ,userinfo.sex ,userinfo.status);
    }
   
    strcpy(datapack.name, "服务器");
    time_t current_time;
    time(&current_time);
    struct tm *local_time = localtime(&current_time);
    strftime(datapack.time, sizeof(datapack.time), "%m/%d %H:%M:%S", local_time);
    datapack.size_num=strlen(datapack.data);
    printf(">>>>>|%d|%s|%s|%d|%s|\n", datapack.SYN, datapack.name, datapack.data, datapack.size_num, datapack.time);
    if (-1 == sendto(CliAddr.UDP_fd, &datapack, sizeof(datapack), 0, (struct sockaddr *)&CliAddr.clientaddr, CliAddr.addrlen))
    {
        ERRLOG("发送失败  \n");
    }
pthread_mutex_unlock(&lock);
    return NULL;
}

// 5、关闭套接字
void UDP_close(int UDP_fd)
{
    close(UDP_fd);
    return;
}

void f_Sign_Print(user_t  user_data) 
{

    FILE *fp=fopen("my_file/user_info.txt","a");
    if (NULL==fp)
    {
        printf("文件打开失败\n");
        return;
    }
    fseek(fp,0,SEEK_END);
    fprintf(fp, "%-15s\t%-15s\t%-15s\t%-15s\t%-5s\t%-5d\n",
                user_data.account, user_data.name, user_data.passwd, user_data.phone,
                user_data.sex, user_data.status);
    fclose(fp);


}


void f_All_Print(void) 
{
    pthread_mutex_lock(&lock);
    FILE *fp=fopen("my_file/user_info.txt","w");
    if (NULL==fp)
    {
        printf("|user_info文件打开失败|\n");
        return;
    }
    u_node * ptr=user->next;
    fseek(fp,0,SEEK_SET);
    for (size_t i = 0; i < user->len; i++)
    {
       fprintf(fp, "%-15s\t%-15s\t%-15s\t%-15s\t%-5s\t%-5d\n",
                ptr->user_data.account,  ptr->user_data.name,  ptr->user_data.passwd,  ptr->user_data.phone,
                 ptr->user_data.sex,  ptr->user_data.status);
        ptr=ptr->next;
    }
    
    
    fclose(fp);
    pthread_mutex_unlock(&lock); 

}


void f_Scanf(void)
{

    FILE *fp=fopen("my_file/user_info.txt","r");
    if (NULL==fp)
    {
        printf("|user_info文件打开失败|\n");
        return;
    }
    fseek(fp,0,SEEK_SET);
    user_t user_data;
    user->len=0;
    while (6==fscanf(fp, "%15s\t%15s\t%15s\t%15s\t%5s\t%5d\n",
                user_data.account, user_data.name, user_data.passwd, user_data.phone,
                user_data.sex,& user_data.status))
    {
        pos_insert_LinkList(user_data, user->len+1 );
    }
    fclose(fp);
 

}

